//
//  WCDelegate.swift
//  NJU QR
//
//  Created by 许煜恒 on 8/16.
//

import Foundation
import WatchConnectivity
import SwiftUI

class WCSendDelegate: NSObject, WCSessionDelegate {
    //Used on iOS
    
    @AppStorage("login_cookie")
    private var login_cookie:String?
    
    
    func sessionDidBecomeInactive(_ session: WCSession) {
        return
    }

    func sessionDidDeactivate(_ session: WCSession) {
        session.activate()
    }
    
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        print("Session activated")
        if activationState == .activated && login_cookie != nil{
            print("About to send login_cookie")
            session.transferUserInfo(["login_cookie":login_cookie!])
        }
    }
    
}

var didTransmission:Bool=false
let delegate=WCSendDelegate()//必须要全局变量！session那边是弱引用，赋值完就释放！！！
func doTransmission(){
    if WCSession.isSupported() && didTransmission==false{
        print("About to activate WCSession")
        let session=WCSession.default
        session.delegate=delegate
        session.activate()
        didTransmission=true
    }
}
